#include <QtGui>
#include "chartTray.h"

ChartTray::ChartTray(QWidget *parent) : QWidget(parent) {
  
  setType(1);
  
  setVar(DEFWIDTH, VAR_B);
  setVar(atan(DEFINCL), VAR_ANGLE);

  setVar(DEFHEIGHT, VAR_H);
  setVar(DEFHEIGHT, VAR_HR);
  setVar(DEFHEIGHT, VAR_HCR);
  setVar(DEFHEIGHT, VAR_HRAIN);

  setVar(0, VAR_QCON);
  setVar(0, VAR_QR);
  setVar(0, VAR_QCR);
  setVar(0, VAR_QRAINCON);

  isVisibleCR   = true;
  isVisibleR    = true;
  isVisibleREC  = true;
  isVisibleRAIN = true;

  setNumPart(0);

} //ChartTray::ChartTray(QWidget *parent = 0) : QWidget(parent)

void ChartTray::paintEvent(QPaintEvent *) {

  QPainter painter(this);
  painter.setRenderHint(QPainter::Antialiasing, true);
  draw(&painter);

} //void ChartTray::paintEvent(QPaintEvent * event)

void ChartTray::draw(QPainter * painter) {

  drawTray(painter);

} //void ChartTray::draw(QPainter * painter)

void ChartTray::drawTray(QPainter * painter) {
 
  int beg = 0;
  int end = 0;

  QBrush brush;

  painter->setBrush(QBrush(QColor(219, 219, 219)));
  painter->drawRect(0+DEFXSHIFTTRAY, 0+DEFYSHIFTTRAY, this->width(), this->height());
  // painter->drawRect(0+DEFXSHIFTTRAY, 0+DEFYSHIFTTRAY, DEFWRECTTRAY, DEFHRECTTRAY);

//Клетки темные
  QPen pen(QColor(180, 180, 180));
  painter->setPen(pen);

  //Y
  beg = DEFXSHIFTTRAY + DEFSIZECELL;
  end = this->width() + DEFYSHIFTTRAY;
  // end = DEFWRECTTRAY + DEFYSHIFTTRAY;
  for (int i=beg; i < end; i+=DEFSIZECELL)
    painter->drawLine(i, DEFYSHIFTTRAY+1, i, DEFYSHIFTTRAY+this->height()-1);
    // painter->drawLine(i, DEFYSHIFTTRAY+1, i, DEFYSHIFTTRAY+DEFHRECTTRAY-1);

  //X
  beg = this->height() + DEFYSHIFTTRAY - DEFSIZECELL;
  // beg = DEFHRECTTRAY + DEFYSHIFTTRAY - DEFSIZECELL;
  end = DEFYSHIFTTRAY;
  for (int j=beg; j > end; j-=DEFSIZECELL)
    painter->drawLine(DEFXSHIFTTRAY+1, j, this->width()+DEFXSHIFTTRAY-1, j);
    // painter->drawLine(DEFXSHIFTTRAY+1, j, DEFWRECTTRAY+DEFXSHIFTTRAY-1, j);

//Клетки светлые
  pen.setColor(QColor(200, 200, 200));
  pen.setStyle(Qt::DashLine);

  painter->setPen(pen);

  //Y
  beg = DEFXSHIFTTRAY + rint(DEFSIZECELL/2);
  end = this->width() + DEFYSHIFTTRAY;
  // end = DEFWRECTTRAY + DEFYSHIFTTRAY;
  for (int i=beg; i < end; i+=DEFSIZECELL)
    painter->drawLine(i, DEFYSHIFTTRAY+1, i, DEFYSHIFTTRAY+this->height()-1);
    // painter->drawLine(i, DEFYSHIFTTRAY+1, i, DEFYSHIFTTRAY+DEFHRECTTRAY-1);

  //X
  beg = this->height() + DEFYSHIFTTRAY - rint(DEFSIZECELL/2);
  // beg = DEFHRECTTRAY + DEFYSHIFTTRAY - rint(DEFSIZECELL/2);
  end = DEFYSHIFTTRAY;
  for (int j=beg; j > end; j-=DEFSIZECELL)
    painter->drawLine(DEFXSHIFTTRAY+1, j, this->width()+DEFXSHIFTTRAY-1, j);
    // painter->drawLine(DEFXSHIFTTRAY+1, j, DEFWRECTTRAY+DEFXSHIFTTRAY-1, j);

  pen.setColor(QColor(100, 100, 100));
  pen.setStyle(Qt::DashLine);
  painter->setPen(pen);

  drawChartTypes(painter);

} //void ChartTray::drawTray(QPainter * painter)

void ChartTray::drawChartTypes(QPainter * painter) {

  QBrush brush;

//Центр
  int XCenter = rint((this->width())/2) + DEFXSHIFTTRAY;
  int YCenter = rint((this->height())/2) + DEFYSHIFTTRAY - 25; 
  // int XCenter = rint((DEFWRECTTRAY)/2) + DEFXSHIFTTRAY;
  // int YCenter = rint((DEFHRECTTRAY)/2) + DEFYSHIFTTRAY - 25; 

//   //Y
//   painter->drawLine(XCenter, DEFYSHIFTTRAY+1, XCenter, DEFHRECTTRAY+DEFYSHIFTTRAY-1);
//   //X
//   painter->drawLine(DEFXSHIFTTRAY+1, YCenter, DEFWRECTTRAY+DEFXSHIFTTRAY-1, YCenter);

//Лоток
  painter->setPen(QPen(QColor(10, 10, 10)));

//Переменные  
  double B	= getVar(VAR_B);
  double L 	= 0;
  double Angle  = getVar(VAR_ANGLE);
  double H 	= getVar(VAR_H);
  double Hr 	= getVar(VAR_HR);
  double Hcr 	= getVar(VAR_HCR);
  double Hrain 	= getVar(VAR_HRAIN);


  if (isVisibleRAIN)
    painter->drawImage(2, 2, QImage(":/resources/hid_qrain.png"));
  else
    painter->drawImage(2, 2, QImage(":/resources/hid_qrain1.png"));

  if (isVisibleCR)
    painter->drawImage(2, 32, QImage(":/resources/hid_qcr.png"));
  else
    painter->drawImage(2, 32, QImage(":/resources/hid_qcr1.png"));

  if (isVisibleR)
    painter->drawImage(DEFWRECTTRAY-42, 2, QImage(":/resources/hid_qr.png"));
  else
    painter->drawImage(DEFWRECTTRAY-42, 2, QImage(":/resources/hid_qr1.png"));

  if (isVisibleREC)
    painter->drawImage(DEFWRECTTRAY-42, 32, QImage(":/resources/hid_qrec.png"));
  else
    painter->drawImage(DEFWRECTTRAY-42, 32, QImage(":/resources/hid_qrec1.png"));

//Проверка условий
  if (getVar(VAR_QR) > getVar(VAR_QCR))
    painter->drawImage(this->width()-152, DEFHRECTTRAY-52-YSMALLCHART+2, QImage(":/resources/conditionQno.png"));
  else
    painter->drawImage(this->width()-152, DEFHRECTTRAY-52-YSMALLCHART+2, QImage(":/resources/conditionQyes.png"));

  if (getVar(VAR_VS) < getVar(VAR_VR)) {

    if (getVar(VAR_VD) < getVar(VAR_VR))
      painter->drawImage(this->width()-152, DEFHRECTTRAY-63-YSMALLCHART+39, QImage(":/resources/conditionV2.png"));
      // painter->drawText(535, 295, "Vs < Vr > Vd");
    else
      painter->drawImage(this->width()-152, DEFHRECTTRAY-63-YSMALLCHART+39, QImage(":/resources/conditionV1.png"));
      // painter->drawText(535, 295, "Vs < Vr < Vd");

  } //if (getVar(VAR_VS) < getVar(VAR_VR))

  else {

    if (getVar(VAR_VD) < getVar(VAR_VR))
      painter->drawImage(this->width()-152, DEFHRECTTRAY-63-YSMALLCHART+39, QImage(":/resources/conditionV4.png"));
      // painter->drawText(535, 295, "Vs > Vr > Vd");
    else
      painter->drawImage(this->width()-152, DEFHRECTTRAY-63-YSMALLCHART+39, QImage(":/resources/conditionV3.png"));

  } //else: if (getVar(VAR_VS) < getVar(VAR_VR))

  // if (getVar(VAR_VS) < getVar(VAR_VR)) {

  //   if (getVar(VAR_VD) < getVar(VAR_VR)) {

  //     painter->drawImage(520, 280, QImage(":/resources/conditionNO.png"));
  //     painter->drawText(535, 295, "Vs < Vr > Vd");

  //   } //if (getVar(VAR_VD) < getVar(VAR_VR))

  //   else {

  //     painter->drawImage(520, 280, QImage(":/resources/conditionYES.png"));
  //     painter->drawText(535, 295, "Vs < Vr < Vd");

  //   } //else: if (getVar(VAR_VD) < getVar(VAR_VR))

  // } //if (getVar(VAR_VS) < getVar(VAR_VR))

  // else {

  //   if (getVar(VAR_VD) < getVar(VAR_VR)) {

  //     painter->drawImage(520, 280, QImage(":/resources/conditionNO.png"));
  //     painter->drawText(535, 295, "Vs > Vr > Vd");

  //   } //if (getVar(VAR_VD) < getVar(VAR_VR))
  //   else {

  //     painter->drawImage(520, 280, QImage(":/resources/conditionNO.png"));
  //     painter->drawText(535, 295, "Vs > Vr < Vd");

  //   } //else: if (getVar(VAR_VD) < getVar(VAR_VR))

  // } //else: if (getVar(VAR_VS) < getVar(VAR_VR))

  if (Hr > H) Hr = H;
  if (Hr < 0) Hr = 0;

  if (Hcr > H) Hcr = H;
  if (Hcr < 0) Hcr = 0;

  if (Hrain > H) Hrain = H;
  if (Hrain < 0) Hrain = 0;

  if (curChartType == 1)
    L = B + 2.2 * (H / sin(Angle));
  else if ((curChartType == 2) || (curChartType == 3))
    L = B + 2 * (H / sin(Angle));
  else if (curChartType == 4)
    L = B + 1.5 * (H / sin(Angle));


//Вычисление пропорций
  //Длина стенок по Ox
  int cD = 70;
  //Длина дна
  int cB     = to_propoptions(B,     L, 320);
  //Полная высота
  int cH     = to_propoptions(H,     L, 420);
  //Расчетная высота
  int cHr    = to_propoptions(Hr,    L, 420);
  //Критическая высота
  int cHcr   = to_propoptions(Hcr,   L, 420);
  //Дождевая высота
  int cHrain = to_propoptions(Hrain, L, 420);

  //Ширина стенок
  int cS = 15;
  //Длина поверхности земли
  int cU = 80;
  //Смещение точек
  double cSh = (double(to_gr(Angle)) / 90);

//Расчет координат
  //Первая тройка
  int TX_11 = 0;
  int TX_12 = 0;
  int TX_13 = 0;
  int TY_11 = 0;
  int TY_12 = 0;
  int TY_13 = 0;
  //Вторая тройка
  int TX_21 = 0;
  int TX_22 = 0;
  int TX_23 = 0;
  int TY_21 = 0;
  int TY_22 = 0;
  int TY_23 = 0;

  double lx11 = 0;
  double ly11 = 0;
  double lx21 = 0;
  double ly21 = 0;

  double Lb1 = 0;
  double Lk1 = 0;

  double lx12 = 0;
  double ly12 = 0;
  double lx22 = 0;
  double ly22 = 0;

  double Lb2 = 0;
  double Lk2 = 0;

  double lx13 = 0;
  double ly13 = 0;
  double lx23 = 0;
  double ly23 = 0;

  double Lb3 = 0;
  double Lk3 = 0;

  if ((curChartType == 1) || (curChartType == 3)) {

    //Первая тройка
    TX_11 = XCenter - rint(cB/2) - cD + rint(cD*cSh) - rint((cS * 2)*cSh);
    TX_12 = XCenter - rint(cB/2) - cD + rint(cD*cSh) - rint((cS * 1)*cSh);
    TX_13 = XCenter - rint(cB/2) - cD + rint(cD*cSh) - rint((cS * 0)*cSh);

    TY_11 = YCenter - rint(cH/2) + 2 * cS;
    TY_12 = YCenter - rint(cH/2) + 1 * cS;
    TY_13 = YCenter - rint(cH/2) + 0 * cS;

    //Вторая тройка
    TX_21 = XCenter - rint(cB/2) - rint((cS * 2)*cSh);
    TX_22 = XCenter - rint(cB/2) - rint((cS * 1)*cSh);
    TX_23 = XCenter - rint(cB/2) - rint((cS * 0)*cSh);

    TY_21 = YCenter + rint(cH/2) + 2 * cS;
    TY_22 = YCenter + rint(cH/2) + 1 * cS;
    TY_23 = YCenter + rint(cH/2) + 0 * cS;

  } //if ((curChartType == 1) || (curChartType == 3))

  else if ((curChartType == 2) || (curChartType == 4)) {

    //Первая тройка
    TX_11 = XCenter - rint(cB/2) - rint((cS * 2));
    TX_12 = XCenter - rint(cB/2) - rint((cS * 1));
    TX_13 = XCenter - rint(cB/2) - rint((cS * 0));

    TY_11 = YCenter - rint(cH/2);
    TY_12 = YCenter - rint(cH/2);
    TY_13 = YCenter - rint(cH/2);

    //Вторая тройка
    TX_21 = XCenter - rint(cB/2) - rint((cS * 2));
    TX_22 = XCenter - rint(cB/2) - rint((cS * 1));
    TX_23 = XCenter - rint(cB/2) - rint((cS * 0));

    TY_21 = YCenter + rint(cH/2) + 2 * cS;
    TY_22 = YCenter + rint(cH/2) + 1 * cS;
    TY_23 = YCenter + rint(cH/2) + 0 * cS;

  } //else if ((curChartType == 2) || (curChartType == 4))

  //Уравнения прямых
    
  //1
  lx11 = TX_11;
  ly11 = TY_11;
  lx21 = TX_21;
  ly21 = TY_21;

  Lb1 = ( ly21 * lx11 - ly11 * lx21) / ( lx11 - lx21 );
  Lk1 = ( ly11 - Lb1 ) / lx11;
    
  //2
  lx12 = TX_12;
  ly12 = TY_12;
  lx22 = TX_22;
  ly22 = TY_22;

  Lb2 = ( ly22 * lx12 - ly12 * lx22) / ( lx12 - lx22 );
  Lk2 = ( ly12 - Lb2 ) / lx12;
    
  //3
  lx13 = TX_13;
  ly13 = TY_13;
  lx23 = TX_23;
  ly23 = TY_23;

  Lb3 = ( ly23 * lx13 - ly13 * lx23) / ( lx13 - lx23 );
  Lk3 = ( ly13 - Lb3 ) / lx13;

  //Третья тройка
  int TX_31 = 0;
  int TX_32 = 0;
  int TX_33 = 0;

  int TY_31 = 0;
  int TY_32 = 0;
  int TY_33 = 0;

  //Четвертая тройка
  int TX_41 = 0;
  int TX_42 = 0;
  int TX_43 = 0;

  int TY_41 = 0;
  int TY_42 = 0;
  int TY_43 = 0;

  double rx11 = 0;
  double ry11 = 0;
  double rx21 = 0;
  double ry21 = 0;
    
  double Rb1 = 0;
  double Rk1 = 0;

  double rx12 = 0;
  double ry12 = 0;
  double rx22 = 0;
  double ry22 = 0;
    
  double Rb2 = 0;
  double Rk2 = 0;

  double rx13 = 0;
  double ry13 = 0;
  double rx23 = 0;
  double ry23 = 0;
    
  double Rb3 = 0;
  double Rk3 = 0;

  if ((curChartType == 1) || (curChartType == 2)) {

    //Третья тройка
    TX_31 = XCenter + rint(cB/2) + rint((cS * 2)*cSh);
    TX_32 = XCenter + rint(cB/2) + rint((cS * 1)*cSh);
    TX_33 = XCenter + rint(cB/2) + rint((cS * 0)*cSh);

    TY_31 = YCenter + rint(cH/2) + 2 * cS;
    TY_32 = YCenter + rint(cH/2) + 1 * cS;
    TY_33 = YCenter + rint(cH/2) + 0 * cS;

    //Четвертая тройка
    TX_41 = XCenter + rint(cB/2) + cD - rint(cD*cSh) + rint((cS * 2)*cSh);
    TX_42 = XCenter + rint(cB/2) + cD - rint(cD*cSh) + rint((cS * 1)*cSh);
    TX_43 = XCenter + rint(cB/2) + cD - rint(cD*cSh) + rint((cS * 0)*cSh);

    TY_41 = YCenter - rint(cH/2) + 2 * cS;
    TY_42 = YCenter - rint(cH/2) + 1 * cS;
    TY_43 = YCenter - rint(cH/2) + 0 * cS;

  } //if ((curChartType == 1) || (curChartType == 2))

  else if ((curChartType == 3) || (curChartType == 4)) {

    //Третья тройка
    TX_31 = XCenter + rint(cB/2) + rint((cS * 2));
    TX_32 = XCenter + rint(cB/2) + rint((cS * 1));
    TX_33 = XCenter + rint(cB/2) + rint((cS * 0));

    TY_31 = YCenter + rint(cH/2) + 2 * cS;;
    TY_32 = YCenter + rint(cH/2) + 1 * cS;;
    TY_33 = YCenter + rint(cH/2) + 0 * cS;;

    //Четвертая тройка
    TX_41 = XCenter + rint(cB/2) + rint((cS * 2));
    TX_42 = XCenter + rint(cB/2) + rint((cS * 1));
    TX_43 = XCenter + rint(cB/2) + rint((cS * 0));

    TY_41 = YCenter - rint(cH/2);
    TY_42 = YCenter - rint(cH/2);
    TY_43 = YCenter - rint(cH/2);

  } //else if ((curChartType == 3) || (curChartType == 4))

  //Уравнения прямых

  //1
  rx11 = TX_31;
  ry11 = TY_31;
  rx21 = TX_41;
  ry21 = TY_41;
    
  Rb1 = ( ry21 * rx11 - ry11 * rx21) / ( rx11 - rx21 );
  Rk1 = ( ry11 - Rb1 ) / rx11;

  //2
  rx12 = TX_32;
  ry12 = TY_32;
  rx22 = TX_42;
  ry22 = TY_42;
    
  Rb2 = ( ry22 * rx12 - ry12 * rx22) / ( rx12 - rx22 );
  Rk2 = ( ry12 - Rb2 ) / rx12;

  //3
  rx13 = TX_33;
  ry13 = TY_33;
  rx23 = TX_43;
  ry23 = TY_43;
    
  Rb3 = ( ry23 * rx13 - ry13 * rx23) / ( rx13 - rx23 );
  Rk3 = ( ry13 - Rb3 ) / rx13;
    
//Бордюры для стенок
  //Левая
  painter->drawLine(TX_13, TY_13, TX_13-cU, TY_13);

  //Правая
  painter->drawLine(TX_43, TY_43, TX_43+cU, TY_43);

//Земля по бокам
  //Левая
  painter->drawLine(TX_13-10, TY_13, TX_13-10 - 15,  TY_13 + 15);
  painter->drawLine(TX_13-20, TY_13, TX_13-20 - 15,  TY_13 + 15);
  painter->drawLine(TX_13-30, TY_13, TX_13-30 - 15,  TY_13 + 15);

  painter->drawLine(TX_13-50, TY_13, TX_13-50 - 15,  TY_13 + 15);
  painter->drawLine(TX_13-60, TY_13, TX_13-60 - 15,  TY_13 + 15);
  painter->drawLine(TX_13-70, TY_13, TX_13-70 - 15,  TY_13 + 15);

  //Правая
  painter->drawLine(TX_43+20, TY_43, TX_43+20 - 15,  TY_43 + 15);
  painter->drawLine(TX_43+30, TY_43, TX_43+30 - 15,  TY_43 + 15);
  painter->drawLine(TX_43+40, TY_43, TX_43+40 - 15,  TY_43 + 15);

  painter->drawLine(TX_43+60, TY_43, TX_43+60 - 15,  TY_43 + 15);
  painter->drawLine(TX_43+70, TY_43, TX_43+70 - 15,  TY_43 + 15);
  painter->drawLine(TX_43+80, TY_43, TX_43+80 - 15,  TY_43 + 15);

//Размерности
  //B
  QString str("b = ");
  str += QString().setNum(getVar(VAR_B));
  str += " м";

  int Hshift = 20;
  painter->drawLine(TX_21, TY_21+Hshift,   TX_31, TY_31+Hshift);
  painter->drawLine(TX_21, TY_21+Hshift+5, TX_21, TY_21+Hshift-5);
  painter->drawLine(TX_31, TY_31+Hshift+5, TX_31, TY_31+Hshift-5); 

  painter->drawText(TX_21 + rint((TX_31-TX_21)/2) - 30, TY_21+Hshift+15, str);

  //H
  str = "h = ";
  str += QString().setNum(getVar(VAR_H));
  str += " м";

  painter->drawLine(TX_13-cU-Hshift, TY_13,  TX_13-cU-Hshift, TY_21);
  painter->drawLine(TX_13-cU-Hshift-5, TY_13, TX_13-cU-Hshift+5, TY_13);
  painter->drawLine(TX_13-cU-Hshift-5, TY_21, TX_13-cU-Hshift+5, TY_21); 

  painter->rotate(-90);
  painter->drawText((TY_13-260) + rint((TY_21-TY_13)/2)-60, TX_13-110, str);
  painter->rotate(90);

  //Angle
  int diameter = 40;

  //Левый
  if ((curChartType == 1) || (curChartType == 3)) {

    str = QString().setNum(to_gr(Angle));
    str += QChar(176);
    painter->drawArc(TX_21-rint(diameter/2), TY_21-rint(diameter/2), diameter, diameter, 180*16, -(to_gr(Angle)+35)*16);
    painter->drawText(TX_21 - 40 - 10*(1-Angle), TY_21 - 10 + 25*(1-Angle), str);

  } //if ((curChartType == 1) || (curChartType == 3))

  else if ((curChartType == 2) || (curChartType == 4)) {

    str = QString().setNum(90);
    str += QChar(176);
    painter->drawLine(TX_21-rint(diameter/2), TY_21, TX_21-rint(diameter/2), TY_21-rint(diameter/2));
    painter->drawLine(TX_21-rint(diameter/2), TY_21-rint(diameter/2), TX_21, TY_21-rint(diameter/2));
    painter->drawText(TX_21 - 40 - 10*(1-Angle), TY_21 - 10 + 25*(1-Angle), str);

  } //else if ((curChartType == 2) || (curChartType == 4))

  //Правый
  if ((curChartType == 1) || (curChartType == 2)) {

    str = QString().setNum(to_gr(Angle));
    str += QChar(176);
    painter->drawArc(TX_31-rint(diameter/2), TY_31-rint(diameter/2), diameter, diameter, 0*16, (to_gr(Angle)+35)*16);
    painter->drawText(TX_31 + 20 + 10*(1-Angle), TY_31 - 10 + 25*(1-Angle), str);

  } //if ((curChartType == 1) || (curChartType == 2))

  else if ((curChartType == 3) || (curChartType == 4)) {

    str = QString().setNum(90);
    str += QChar(176);
    painter->drawLine(TX_31+rint(diameter/2), TY_31, TX_31+rint(diameter/2), TY_31-rint(diameter/2));
    painter->drawLine(TX_31+rint(diameter/2), TY_31-rint(diameter/2), TX_31, TY_31-rint(diameter/2));
    painter->drawText(TX_31 + 20 + 10*(1-Angle), TY_31 - 10 + 25*(1-Angle), str);

  } //else if ((curChartType == 3) || (curChartType == 4))

  //Направляющие для угла
  painter->setPen(Qt::DashLine);

  painter->drawLine(TX_21-5, TY_21, TX_21-25, TY_21);
  painter->drawLine(TX_31+5, TY_31, TX_31+25, TY_31);

  painter->setPen(Qt::SolidLine);


  //Номер участка
  str  = "Номер участка: ";
  str += QString().setNum(numPart);
  painter->drawText(270, this->height()-15, str);


//Уровень воды

  int dLine = 0;
  int dY = 0;
  int walkY = 0;
  int walkX = 0;
  int walkX1 = 0;
  int walkX2 = 0;

//Расчетный
  int Yr = TY_23 - cHr;
  
  if (isVisibleR)
    painter->setPen(QColor(QColor(0, 0, 51)));
  else
    painter->setPen(QColor(190, 190, 190));

  switch (curChartType) {

    case 1: painter->drawLine((Yr - Lb3) / Lk3, Yr, (Yr - Rb3) / Rk3, Yr); break;
    case 2: painter->drawLine( lx13           , Yr, (Yr - Rb3) / Rk3, Yr); break;
    case 3: painter->drawLine((Yr - Lb3) / Lk3, Yr,  rx13           , Yr); break;
    case 4: painter->drawLine( lx13           , Yr,  rx13           , Yr); break;

  } //switch (curChartType)

  str = "Qr = ";
  str += QString().setNum(toPrec(getVar(VAR_QR)), 'g', STRPREC);
  str += " м3/с";

  dLine = rint( 80 + (40 * sin(  double(cB) / (to_propoptions(DEFMAXBSP, L, 320))  )   )  );

  walkX1 = rint( (TX_33 - XCenter) / 2 + 10 * sin( double(cHr) / (double(cH)) ) );
  
  if ((curChartType == 1) || (curChartType == 2))
    walkX2 = ((Yr - Rb3) / Rk3) - XCenter;
  else if ((curChartType == 3) || (curChartType == 4))
    walkX2 = rx13 - XCenter;

  if (walkX1 < walkX2) walkX = walkX1;
  else	       	       walkX = walkX2;

  dY    = TY_43;
  walkY = -rint( 80 * (double(TY_43-10) / double(YCenter))  );

  if (isVisibleR)
    painter->setPen(QColor(0, 0, 51));
  else
    painter->setPen(QColor(190, 190, 190));

  painter->drawText(XCenter+dLine-70+2+walkX, dY+walkY-5, str);
  painter->drawLine(XCenter+dLine+walkX, dY+walkY, XCenter+walkX, dY+walkY);
  painter->drawLine(XCenter+walkX, dY+walkY, XCenter+walkX, Yr);

//Рекомендуемый
  int Yrec = 0;
  
  if (curChartType == 4)
    Yrec = TY_23 - cH*0.75;

  else if ((curChartType == 1) || (curChartType == 2) || (curChartType == 3))
    Yrec = TY_23 - cH*0.70;

  if (isVisibleREC)
    painter->setPen(QColor(51, 36, 0));
  else
    painter->setPen(QColor(190, 190, 190));

  switch (curChartType) {

    case 1: painter->drawLine((Yrec - Lb3) / Lk3, Yrec, (Yrec - Rb3) / Rk3, Yrec); break;
    case 2: painter->drawLine( lx13             , Yrec, (Yrec - Rb3) / Rk3, Yrec); break;
    case 3: painter->drawLine((Yrec - Lb3) / Lk3, Yrec,  rx13             , Yrec); break;
    case 4: painter->drawLine( lx13             , Yrec,  rx13             , Yrec); break;

  } //switch (curChartType)

  str = "Qrec = ";

  if (curChartType == 4)
    str += QString().setNum(toPrec(getVar(VAR_QCON)*0.75), 'g', STRPREC);

  else if ((curChartType == 1) || (curChartType == 2) || (curChartType == 3))
    str += QString().setNum(toPrec(getVar(VAR_QCON)*0.7), 'g', STRPREC);

  str += " м3/с";

  dLine = rint( 140 + (20 * (1-  double(cB) / (to_propoptions(DEFMAXBSP, L, 320))  )   )  );

  walkX1 = rint( (TX_33 - XCenter) / 2 + 10 * sin(double(cHr)/(double(cH+20)) ) ) + 20 * cos(double(cB)/(to_propoptions(DEFMAXBSP, L, 320)));
 
  if ((curChartType == 1) || (curChartType == 2))
    walkX2 = ((Yrec - Rb3) / Rk3) - XCenter;
  else if ((curChartType == 3) || (curChartType == 4))
    walkX2 = rx13 - XCenter;

  if (walkX1 < walkX2) walkX = walkX1;
  else	       	       walkX = walkX2;

  dY    = TY_43+3;
  walkY = -rint( 40 * sin(double(TY_43-10) / double(YCenter))  );

  if (isVisibleREC)
    painter->setPen(QColor(51, 36, 0));
  else
    painter->setPen(QColor(190, 190, 190));

  painter->drawText(XCenter+dLine-70+2+walkX, dY+walkY-5, str);
  painter->drawLine(XCenter+dLine+walkX, dY+walkY, XCenter+walkX, dY+walkY);
  painter->drawLine(XCenter+walkX, dY+walkY, XCenter+walkX, Yrec);

//Критический
  int Ycr = TY_23 - cHcr;

  if (isVisibleCR)
    painter->setPen(QColor(51, 0, 0));
  else
    painter->setPen(QColor(190, 190, 190));

  switch (curChartType) {

    case 1: painter->drawLine((Ycr - Lb3) / Lk3, Ycr, (Ycr - Rb3) / Rk3, Ycr); break;
    case 2: painter->drawLine( lx13            , Ycr, (Ycr - Rb3) / Rk3, Ycr); break;
    case 3: painter->drawLine((Ycr - Lb3) / Lk3, Ycr,  rx13            , Ycr); break;
    case 4: painter->drawLine( lx13            , Ycr,  rx13            , Ycr); break;

  } //switch (curChartType)

  str = "Qcr = ";
  str += QString().setNum(toPrec(getVar(VAR_QCR)), 'g', STRPREC);
  str += " м3/с";

  dLine = -rint( 140 + (20 * (1-  double(cB) / (to_propoptions(DEFMAXBSP, L, 320))  )   )  );

  walkX1 = rint( (TX_23 - XCenter) / 2 - 10 * sin(double(cHr)/(double(cH+20)) ) ) - 20 * cos(double(cB)/(to_propoptions(DEFMAXBSP, L, 320)));
  
  if ((curChartType == 1) || (curChartType == 3))
    walkX2 = ((Ycr - Lb3) / Lk3) - XCenter;
  else if ((curChartType == 2) || (curChartType == 4))
    walkX2 = lx13 - XCenter;

  if (walkX1 > walkX2) walkX = walkX1;
  else	       	       walkX = walkX2;

  dY    = TY_13+3;
  walkY = -rint( 40 * sin(double(TY_13-10) / double(YCenter))  );

  if (isVisibleCR)
    painter->setPen(QColor(51, 0, 0));
  else
    painter->setPen(QColor(190, 190, 190));

  painter->drawText(XCenter+dLine-2+walkX, dY+walkY-5, str);
  painter->drawLine(XCenter+dLine+walkX, dY+walkY, XCenter+walkX, dY+walkY);
  painter->drawLine(XCenter+walkX, dY+walkY, XCenter+walkX, Ycr);

//Дождевой
  int Yrain = TY_23 - cHrain;
  if (isVisibleRAIN)
    painter->setPen(QColor(0, 51, 0));
  else
    painter->setPen(QColor(190, 190, 190));

  switch (curChartType) {

    case 1: painter->drawLine((Yrain - Lb3) / Lk3, Yrain, (Yrain - Rb3) / Rk3, Yrain); break;
    case 2: painter->drawLine( lx13              , Yrain, (Yrain - Rb3) / Rk3, Yrain); break;
    case 3: painter->drawLine((Yrain - Lb3) / Lk3, Yrain,  rx13              , Yrain); break;
    case 4: painter->drawLine( lx13              , Yrain,  rx13              , Yrain); break;

  } //switch (curChartType)

  str = "Qrain = ";
  str += QString().setNum(toPrec(getVar(VAR_QRAINCON)), 'g', STRPREC);
  str += " м3/с";

  dLine = -rint( 80 + (40 * sin(  double(cB) / (to_propoptions(DEFMAXBSP, L, 320))  )   )  );

  walkX1 = rint( (TX_23 - XCenter) / 2 - 10 * sin( double(cHr) / (double(cH)) ) );
  
  if ((curChartType == 1) || (curChartType == 3))
    walkX2 = ((Yrain - Lb3) / Lk3) - XCenter;
  else if ((curChartType == 2) || (curChartType == 4))
    walkX2 = lx13 - XCenter;

  if (walkX1 > walkX2) walkX = walkX1;
  else	       	       walkX = walkX2;

  dY    = TY_13;
  walkY = -rint( 80 * (double(TY_13-10) / double(YCenter))  );

  if (isVisibleRAIN)
    painter->setPen(QColor(0, 51, 0));
  else
    painter->setPen(QColor(190, 190, 190));


  painter->drawText(XCenter+dLine-30-2+walkX, dY+walkY-5, str);
  painter->drawLine(XCenter+dLine+walkX, dY+walkY, XCenter+walkX, dY+walkY);
  painter->drawLine(XCenter+walkX, dY+walkY, XCenter+walkX, Yrain);
  

//Лоток
  QPolygon pol1(8);
  QPolygon pol2(8);

  if (curChartType == 1) {

    pol1.setPoint(0, (TY_13 - Lb2) / Lk2, TY_13);
    pol1.setPoint(1, TX_13, TY_13);
    pol1.setPoint(2, TX_23, TY_23);
    pol1.setPoint(3, TX_33, TY_33);
    pol1.setPoint(4, TX_43, TY_43);
    pol1.setPoint(5, (TY_43 - Rb2) / Rk2, TY_43);
    pol1.setPoint(6, TX_32, TY_32);
    pol1.setPoint(7, TX_22, TY_22);

    pol2.setPoint(0, (TY_13 - Lb1) / Lk1, TY_13);
    pol2.setPoint(1, (TY_13 - Lb2) / Lk2, TY_13);
    pol2.setPoint(2, TX_22, TY_22);
    pol2.setPoint(3, TX_32, TY_32);
    pol2.setPoint(4, (TY_43 - Rb2) / Rk2, TY_43);
    pol2.setPoint(5, (TY_43 - Rb1) / Rk1, TY_43);
    pol2.setPoint(6, TX_31, TY_31);
    pol2.setPoint(7, TX_21, TY_21);

  } //if (curChartType == 1)

  else if (curChartType == 2) {

    pol1.setPoint(0, TX_12, TY_12);
    pol1.setPoint(1, TX_13, TY_13);
    pol1.setPoint(2, TX_23, TY_23);
    pol1.setPoint(3, TX_33, TY_33);
    pol1.setPoint(4, TX_43, TY_43);
    pol1.setPoint(5, (TY_43 - Rb2) / Rk2, TY_43);
    pol1.setPoint(6, TX_32, TY_32);
    pol1.setPoint(7, TX_22, TY_22);

    pol2.setPoint(0, TX_11, TY_11);
    pol2.setPoint(1, TX_12, TY_12);
    pol2.setPoint(2, TX_22, TY_22);
    pol2.setPoint(3, TX_32, TY_32);
    pol2.setPoint(4, (TY_43 - Rb2) / Rk2, TY_43);
    pol2.setPoint(5, (TY_43 - Rb1) / Rk1, TY_43);
    pol2.setPoint(6, TX_31, TY_31);
    pol2.setPoint(7, TX_21, TY_21);

  } //else if (curChartType == 2)

  else if (curChartType == 3) {

    pol1.setPoint(0, (TY_13 - Lb2) / Lk2, TY_13);
    pol1.setPoint(1, TX_13, TY_13);
    pol1.setPoint(2, TX_23, TY_23);
    pol1.setPoint(3, TX_33, TY_33);
    pol1.setPoint(4, TX_43, TY_43);
    pol1.setPoint(5, TX_42, TY_42);
    pol1.setPoint(6, TX_32, TY_32);
    pol1.setPoint(7, TX_22, TY_22);

    pol2.setPoint(0, (TY_13 - Lb1) / Lk1, TY_13);
    pol2.setPoint(1, (TY_13 - Lb2) / Lk2, TY_13);
    pol2.setPoint(2, TX_22, TY_22);
    pol2.setPoint(3, TX_32, TY_32);
    pol2.setPoint(4, TX_42, TY_42);
    pol2.setPoint(5, TX_41, TY_41);
    pol2.setPoint(6, TX_31, TY_31);
    pol2.setPoint(7, TX_21, TY_21);

  } //else if (curChartType == 3)

  else if (curChartType == 4) {

    pol1.setPoint(0, TX_12, TY_12);
    pol1.setPoint(1, TX_13, TY_13);
    pol1.setPoint(2, TX_23, TY_23);
    pol1.setPoint(3, TX_33, TY_33);
    pol1.setPoint(4, TX_43, TY_43);
    pol1.setPoint(5, TX_42, TY_42);
    pol1.setPoint(6, TX_32, TY_32);
    pol1.setPoint(7, TX_22, TY_22);

    pol2.setPoint(0, TX_11, TY_11);
    pol2.setPoint(1, TX_12, TY_12);
    pol2.setPoint(2, TX_22, TY_22);
    pol2.setPoint(3, TX_32, TY_32);
    pol2.setPoint(4, TX_42, TY_42);
    pol2.setPoint(5, TX_41, TY_41);
    pol2.setPoint(6, TX_31, TY_31);
    pol2.setPoint(7, TX_21, TY_21);

  } //else if (curChartType == 4)

  painter->setPen(QColor(Qt::black));
  painter->setBrush(QBrush(QColor(150, 150, 150)));
  painter->drawPolygon(pol1);

  brush.setTextureImage(QImage(":/resources/txtrPrep.png"));

  painter->setBrush(brush);
  painter->drawPolygon(pol2);

  switch(curChartType) {

     case 1: painter->drawImage(XSMALLCHART, DEFHRECTTRAY-75-YSMALLCHART, QImage(":/resources/smallTray1.png")); break;
     case 2: painter->drawImage(XSMALLCHART, DEFHRECTTRAY-75-YSMALLCHART, QImage(":/resources/smallTray2.png")); break;
     case 3: painter->drawImage(XSMALLCHART, DEFHRECTTRAY-75-YSMALLCHART, QImage(":/resources/smallTray3.png")); break;
     case 4: painter->drawImage(XSMALLCHART, DEFHRECTTRAY-75-YSMALLCHART, QImage(":/resources/smallTray4.png")); break;

  } //switch(curChartType)
  
} //void ChartTray::drawChartTypes(QPainter * painter)

void ChartTray::mousePressEvent(QMouseEvent * event) {

  int pressX = event->x() - XSMALLCHART;
  int pressY = event->y() - DEFHRECTTRAY + 100 + YSMALLCHART;

  if (event->buttons() & Qt::LeftButton) {

    if ((pressY >= 0) && (pressY <= 100)) {

      if ((pressX >= 0)  && (pressX < 75)) { 

    	switch (curChartType) {

    	   case 1: curChartType = 2; emit changeTrayType(ASYMTTRAPEZE); break;
    	   case 2: curChartType = 1; emit changeTrayType(SYMTTRAPEZE); break;
    	   case 3: curChartType = 4; emit changeTrayType(RECTANGLE); break;
    	   case 4: curChartType = 3; emit changeTrayType(ASYMTTRAPEZE); break;
	
    	} //switch (curChartType)

      } //if ((pressX >= 0)  && (pressX < 75))

      else if ((pressX >= 75)  && (pressX < 150 )) { 

    	switch (curChartType) {

    	   case 1: curChartType = 3; emit changeTrayType(ASYMTTRAPEZE); break;
    	   case 2: curChartType = 4; emit changeTrayType(RECTANGLE); break;
    	   case 3: curChartType = 1; emit changeTrayType(SYMTTRAPEZE); break;
    	   case 4: curChartType = 2; emit changeTrayType(ASYMTTRAPEZE); break;
	
    	} //switch (curChartType)

      } //else if ((pressX >= 75)  && (pressX < 150))

    } //if ((pressY >= 0) && (pressY <= 100))

    //Видимость дождевой высоты или расхода воды
    if ((event->x() >= 2) && (event->x() <= 32) && (event->y() >= 2) && (event->y() < 32)) {

      if (isVisibleRAIN)
	isVisibleRAIN = false;
      else
	isVisibleRAIN = true;

      update();

    } //if ((event->x() >= 2) && (event->x() <= 32) && (event->y() >= 2) && (event->y() < 32))

    //Видимость критической высоты или расхода воды
    if ((event->x() >= 2) && (event->x() <= 32) && (event->y() >= 32) && (event->y() < 62)) {

      if (isVisibleCR)
	isVisibleCR = false;
      else
	isVisibleCR = true;

      update();

    } //if ((event->x() >= 2) && (event->x() <= 32) && (event->y() >= 32) && (event->y() < 62))

    //Видимость расчетной высоты или расхода воды
    if ((event->x() >= DEFWRECTTRAY-42) && (event->x() <= DEFWRECTTRAY-12) && (event->y() >= 2) && (event->y() < 32)) {

      if (isVisibleR)
	isVisibleR = false;
      else
	isVisibleR = true;

      update();

    } //if ((event->x() >= DEFWRECTTRAY-42) && (event->x() <= DEFWRECTTRAY-12) && (event->y() >= 2) && (event->y() < 32))

    //Видимость рекомендуемой высоты или расхода воды
    if ((event->x() >= DEFWRECTTRAY-42) && (event->x() <= DEFWRECTTRAY-12) && (event->y() >= 32) && (event->y() < 62)) {

      if (isVisibleREC)
	isVisibleREC = false;
      else
	isVisibleREC = true;

      update();

    } //if ((event->x() >= DEFWRECTTRAY-42) && (event->x() <= DEFWRECTTRAY-12) && (event->y() >= 32) && (event->y() < 62))

  // painter->drawRect(2,  2, 32, 32);
  // painter->drawRect(2, 32, 32, 32);
  // painter->drawRect(2, 62, 32, 32);
  // painter->drawRect(2, 92, 32, 32);


  } //if (event->buttons() & Qt::LeftButton)

} //void ChartTray::mousePressEvent(QMouseEvent *event)

// void ChartTray::mouseMoveEvent(QMouseEvent * event) {

//   int pressX = event->x() - XSMALLCHART;
//   int pressY = event->y() - DEFHRECTTRAY + 100 + YSMALLCHART;

//   if ( (pressY >= 0) && (pressY <= 100) && (pressX >= 0) && (pressX <= 150 ) ) {

//     QWidget::setCursor(QCursor(Qt::PointingHandCursor));

//   } //if ( ((pressY >= 0) && (pressY <= 100)) && ((pressX >= 0)  && (pressX < 150 )))

//   else {

//     QWidget::setCursor(QCursor(Qt::ArrowCursor));

//   } //else: if ( ((pressY >= 0) && (pressY <= 100)) && ((pressX >= 0)  && (pressX < 150 )))

// } //void ChartTray::mouseMoveEvent(QMouseEvent * event)


void ChartTray::setVar(double newVal, int varName) {

  switch (varName) {

     case VAR_ANGLE:	insAngle = newVal; update(); break;
     case VAR_B:      	insB     = newVal; update(); break;
     case VAR_H:      	insH     = newVal; update(); break;
     case VAR_HR:     	insHr    = newVal; update(); break;
     case VAR_HCR:    	insHcr   = newVal; update(); break;
     case VAR_HRAIN:  	insHrain = newVal; update(); break;
     case VAR_QCON:    	insQ     = newVal; update(); break;
     case VAR_QR:     	insQr    = newVal; update(); break;
     case VAR_QCR:    	insQcr   = newVal; update(); break;
     case VAR_QRAINCON:	insQrain = newVal; update(); break;
     case VAR_VS:     	insVs    = newVal; update(); break;
     case VAR_VR:     	insVr    = newVal; update(); break;
     case VAR_VD:     	insVd    = newVal; update(); break;

  } //switch (varName)

} //void ChartTray::setVar(double newVal, int varName)

double ChartTray::getVar(int varName) {

  switch (varName) {

     case VAR_ANGLE:	return insAngle; break;
     case VAR_B:      	return insB;     break;
     case VAR_H:      	return insH;     break;
     case VAR_HR:     	return insHr;    break;
     case VAR_HCR:    	return insHcr;   break;
     case VAR_HRAIN:  	return insHrain; break;
     case VAR_QCON:    	return insQ;     break;
     case VAR_QR:     	return insQr;    break;
     case VAR_QCR:    	return insQcr;   break;
     case VAR_QRAINCON: return insQrain; break;
     case VAR_VS:     	return insVs;    break;
     case VAR_VR:     	return insVr;    break;
     case VAR_VD:     	return insVd;    break;

  } //switch (varName)

  return 0;

} //double ChartTray::getVar(int varName)

//Перевод реальных значений в пропорции
int ChartTray::to_propoptions(double realVal, double L, int maxVal) { 

  return rint(maxVal * (realVal / L)); 

} //int ChartTray::to_propoptions(double realVal, double L, int maxVal)

//Перевод из пропорций в реальную величину
double ChartTray::to_realVal(int propoptions, double L, int maxVal) { 

  return ((propoptions / maxVal) * L); 

} //double ChartTray::to_realVal(int propoptions, double L, int maxVal)
